package com.e3.core.base;

import java.util.List;

import org.slf4j.Logger;

import com.e3.core.cache.Cache;
import com.e3.core.cache.CacheManager;
import com.e3.sysmg.audit.log.MyLoggerFactory;
import com.e3.web.core.model.SuperModel;

public class ModelCacheManager<D extends SuperModel> {
	
	private static Logger logger = MyLoggerFactory.getLogger(ModelCacheManager.class);
	
	public void putModel(String cacheName,D model){
		Cache<D> cache = CacheManager.getCache(cacheName);
		cache.put(model.getId(), model);
	}
	
	public void removeModels(String cacheName,List<Long> ids){
		if(cacheName==null||cacheName.trim().length()==0
				||ids==null||ids.isEmpty()){
			return;
		}
		Cache<D> cache = CacheManager.getCache(cacheName);
		if(cache==null){
			logger.error("invalid cache name {}",cacheName);
			return;
		}
		for (Long id : ids) {
			cache.remove(id);
		}
	}
	
	public D getModel(String cacheName,Long id){
		Cache<D> cache = CacheManager.getCache(cacheName);
		return cache.get(id);
	}
}
